﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Models;
using BLL;
using System.IO;

namespace Dryricenet.houTai
{
    public partial class food_Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Xlk();
                int id =Convert.ToInt32( Request.QueryString["FoodID"].ToString());
                //根据id查询数据，并展示在输入框中
                DataTable dt = foodBLL.selectIdBLL(id);
                //用DataRow来存一行数据
                DataRow dr = dt.Rows[0];//第一条数据
                                        //把数据一个一个的放进输入框里
                txtID.Text = id.ToString();
                txtName.Text = dr["FoodName"].ToString();
                DropDownList1.SelectedIndex =Convert.ToInt32(dr["FoodTypeID"].ToString())-1;
                txtNR.Text = dr["Describe"].ToString();
                Image1.ImageUrl= dr["PicUrl"].ToString();
                txtDJ.Text = dr["Price"].ToString();
                txtDW.Text = dr["Fen"].ToString();
                drTJ.Text = dr["Special"].ToString();
                txtXS.Text = dr["SalesVolume"].ToString();
                txtKC.Text = dr["Inventory"].ToString();
            }
        }
        //下拉框
        public void Xlk()
        {
            DropDownList1.DataSource = foodTypeBLL.liXlkBLL();
            DropDownList1.DataTextField = "FoodTypeName";
            DropDownList1.DataValueField = "FoodTypeID";
            DropDownList1.DataBind();
        }

        protected void Add_Button_Click(object sender, EventArgs e)
        {
            Food f = new Food();
            f.FoodID =Convert.ToInt32(txtID.Text.Trim());
            f.FoodName = txtName.Text.Trim();
            f.FoodTypeID = DropDownList1.SelectedIndex + 1;

            if (FileUpload1.HasFile)
            {
                if (CheckFileType(FileUpload1.FileName))
                {
                    string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Path.GetExtension(FileUpload1.FileName);
                    string filePath = "~/image/" + fileName;
                    FileUpload1.SaveAs(MapPath(filePath));
                    f.PicUrl = filePath;
                }
            }
            else {
                f.PicUrl = Image1.ImageUrl;
            }
            f.Price = Convert.ToDouble(txtDJ.Text.Trim());
            f.Fen = txtDW.Text.Trim();
            f.SalesVolume = Convert.ToInt32(txtXS.Text.Trim());
            f.Inventory = Convert.ToInt32(txtKC.Text.Trim());
            f.Special = drTJ.SelectedValue;
            f.Describe = txtNR.Text.Trim();
            f.Time = DateTime.Now;
            if (foodBLL.updateFoodBLL(f))
            {
                Response.Write("<script>javascript:alert('修改成功!');history.back();</script>");
                Response.End();
            }
            else
            {
                Response.Write("<script>javascript:alert('修改失败!');history.back();</script>");
                Response.End();
            }
        }
        public bool CheckFileType(string filename)
        {
            string ext = Path.GetExtension(filename);
            switch (ext.ToLower())
            {
                case ".gif": return true;
                case ".png": return true;
                case ".jpg": return true;
                case ".jpeg": return true;
                default: return false;
            }
        }
    }
}